package de.wbh.gwt.form.layouter.gwt.ui.view;

import java.util.List;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.drop.CustomDropController;
import com.google.common.collect.Lists;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;

import de.wbh.gwt.form.layouter.gwt.rpc.PcfService.PositionedField;
import de.wbh.gwt.form.layouter.gwt.ui.view.widget.Field;
import de.wbh.gwt.form.layouter.shared.Box;
import de.wbh.gwt.form.layouter.shared.activity.EditPositionsView;

public class EditPositionsViewImpl extends Composite implements EditPositionsView {

	interface EditPositionsViewImplUiBinder extends UiBinder<Widget, EditPositionsViewImpl> {
	}

	private static EditPositionsViewImplUiBinder uiBinder = GWT.create(EditPositionsViewImplUiBinder.class);

	private Presenter presenter;

	@UiField
	Image image;

	@UiField
	AbsolutePanel dropLayer;

	@UiField
	ScrollPanel scrollPanel;

	private CustomDropController dropController;

	private PickupDragController dragController;

	public EditPositionsViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		this.image.addLoadHandler(new LoadHandler() {

			@Override
			public void onLoad(LoadEvent event) {
				System.out.println("imageWidth: " + EditPositionsViewImpl.this.image.getWidth());
				System.out.println("imageHeight: " + EditPositionsViewImpl.this.image.getHeight());
				if (EditPositionsViewImpl.this.image.getWidth() > 1000) {
					EditPositionsViewImpl.this.image.setWidth(EditPositionsViewImpl.this.image.getWidth() / 2 + "px");
					// EditPositionsViewImpl.this.image.setHeight(EditPositionsViewImpl.this.image.getHeight() / 2 + "px");
				}
			}
		});

		this.scrollPanel.addScrollHandler(new ScrollHandler() {

			@Override
			public void onScroll(ScrollEvent event) {
				int scrollPos = EditPositionsViewImpl.this.scrollPanel.getVerticalScrollPosition();
				System.out.println("scrollPos: " + scrollPos);
				EditPositionsViewImpl.this.image.getElement().getStyle().setTop(scrollPos, Unit.PX);
			}
		});
	}

	@Override
	public void addFields(List<PositionedField> result) {
		List<Field> fields = Lists.newArrayList();
		for (PositionedField positionedField : result) {
			Field field = new Field(positionedField.id);
			field.setPixelSize(positionedField.width, positionedField.height);
			this.dropLayer.add(field, positionedField.x, positionedField.y);
			fields.add(field);
		}
		this.presenter.fieldsCreated(fields);
	}

	@Override
	public void dragControllerAdded(PickupDragController dragController) {
		if (this.dropController != null) {
			dragController.registerDropController(this.dropController);
			System.err.println("REGISTERED");
		} else {
			this.dragController = dragController;
		}
	}

	@Override
	public List<PositionedField> getPositionedFields() {
		List<PositionedField> positionedFields = Lists.newArrayList();
		for (Widget w : EditPositionsViewImpl.this.dropLayer) {
			if (w instanceof Field) {
				PositionedField pf = new PositionedField();
				pf.id = ((Field) w).getId();
				pf.x = EditPositionsViewImpl.this.dropLayer.getWidgetLeft(w);
				pf.y = EditPositionsViewImpl.this.dropLayer.getWidgetTop(w);
				pf.width = w.getOffsetWidth();
				pf.height = w.getOffsetHeight();
				positionedFields.add(pf);
			}
		}
		return positionedFields;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void showBoxes(List<Box> boxes) {
		for (Box box : boxes) {
			box.x = box.x / 2;
			box.y = box.y / 2;
			box.width = box.width / 2;
			box.height = box.height / 2;
		}
		this.dropController = new CustomDropController(this.dropLayer, boxes) {
			@Override
			public void onDrop(DragContext context) {
				super.onDrop(context);
				List<PositionedField> positionedFields = getPositionedFields();
				EditPositionsViewImpl.this.presenter.updatePositions(positionedFields);
			}
		};
		if (this.dragController != null) {
			this.dragController.registerDropController(this.dropController);
		} else {
			this.presenter.dropControllerAdded(this.dropController);
		}

	}

	@Override
	public void showImage(String url) {
		this.image.setUrl(url);
	}

}
